package com.ByteDance.controller;


import com.ByteDance.model.po.UserInfos;
import com.ByteDance.model.vo.User;
import com.ByteDance.model.vo.UserInfosVO;
import com.ByteDance.model.vo.UserVO;
import com.ByteDance.service.IUserService;
import com.ByteDance.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 朱哥
 * @since 2024-02-20
 */
@RestController
@Api(tags="用户Controller")
@Slf4j
@RequestMapping("/douyin")
@RequiredArgsConstructor //必备的构造函数，只会对一开始需要初始化的变量去做构造
public class UserController {
    private final IUserService iUserService;

    @ApiOperation("用户注册接口")
    @PostMapping("/user/register")
    public UserVO userRegister(@RequestParam(value = "username")String username,@RequestParam(value = "password")String password){
        //处理注册信息的逻辑
        //1.实现注册
        UserInfos user = iUserService.register(username, password);

        //注册失败（传入的用户名或密码为空）/ 已经注册过  -- 这两种情况会传回null
        if(user == null){
            return UserVO.fail();
        }

        //注册成功，构建UserVO进行返回
        UserVO userVO = UserVO.success();//new一个UserVO对象出来并且设置VO的状态码和状态信息
        Map<String,Object> claims = new HashMap<>();//封装了JWT当中封装的自定义数据
        claims.put("userId",user.getUserId());
        claims.put("username",user.getUsername());
        String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
        userVO.setToken(jwt);
        userVO.setUserId(user.getUserId());
        return  userVO;
    }

    @ApiOperation("用户登录接口")
    @PostMapping("/user/login")
    public UserVO userLogin(@RequestParam(value = "username")String username,@RequestParam(value = "password")String password){
        //处理登录的逻辑
        //1.实现登录
        UserInfos user = iUserService.login(username, password);

        //登录失败（传入的用户名或密码为空）/ 没有注册  -- 这两种情况会传回null
        if(user == null){
            return UserVO.fail();
        }

        //登录成功，构建UserVO进行返回
        UserVO userVO = UserVO.success();//new一个UserVO对象出来并且设置VO的状态码和状态信息
        Map<String,Object> claims = new HashMap<>();//封装了JWT当中封装的自定义数据
        claims.put("userId",user.getUserId());
        claims.put("username",user.getUsername());
        String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
        userVO.setToken(jwt);
        userVO.setUserId(user.getUserId());
        return  userVO;
    }

    @ApiOperation("查询用户信息接口")
    @GetMapping("/user")
    public UserInfosVO getUserInfoById(@RequestParam(value = "user_id") Integer userId){
        UserInfos userInfos = iUserService.getById(userId);
        if (userInfos == null){
            return UserInfosVO.fail();//类名.方法名（静态方法的调用）
        }
        //构建UserInfosVO返回
        UserInfosVO vo = UserInfosVO.success();
        vo.setUser(new User());
        vo.getUser().setUserId(userInfos.getUserId());
        vo.getUser().setUsername(userInfos.getUsername());
        vo.getUser().setFollowCount(userInfos.getFollowCount());
        vo.getUser().setFollowerCount(userInfos.getFollowerCount());
        //自己不需要关注自己
        vo.getUser().setFollow(false);
        vo.getUser().setAvatar(userInfos.getAvatar());
        vo.getUser().setBackgroundImage(userInfos.getBackgroundImage());
        vo.getUser().setSignature(userInfos.getSignature());
        vo.getUser().setTotalFavorited(userInfos.getTotalFavorited());
        vo.getUser().setWorkCount(userInfos.getWorkCount());
        vo.getUser().setFavoriteCount(userInfos.getFavoriteCount());
        return vo;
    }
}
